﻿#include "treeinfoitem.h"
#include "ui_treeinfoitem.h"



TreeInfoItem::TreeInfoItem(QMenu* itemMenu, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TreeInfoItem)
{
    ui->setupUi(this);
    menu = itemMenu;
    idM = -2;
    idW = -2;
    ui->M->hide();
    ui->W->hide();
    ui->SPW->hide();
}

TreeInfoItem::~TreeInfoItem()
{
    delete ui;
}

void TreeInfoItem::setIdM(int id){
    idM = id;
}

void TreeInfoItem::setIdW(int id){
    idW = id;
}

void TreeInfoItem::setNameM(QString name){
    ui->M->setText(name);
    ui->M->show();
}

void TreeInfoItem::setNameW(QString name){
    ui->W->setText(name);
    ui->W->show();
    if(idM>0){
        ui->SPW->show();
    }
}

QString TreeInfoItem::nameM(){
    return ui->M->text();
}

QString TreeInfoItem::nameW(){
    return ui->W->text();
}

int TreeInfoItem::getIDM(){
    return idM;
}

int TreeInfoItem::getIDW(){
    return idW;
}

void TreeInfoItem::clear(){
    ui->M->hide();
    ui->W->hide();
    ui->SPW->hide();
}

//重载鼠标双击事件，使得双击控件后得以显示详情页
void TreeInfoItem::mouseDoubleClickEvent(QMouseEvent *event){
    //判断鼠标点击的区域是男还是女
    if(ui->M->isVisible()&&ui->M->geometry().contains(event->pos())){
        qDebug()<<"M D";
        //通过设置鼠标右键菜单中不可见的最后一项的文字为ID，以此传递ID信息
        menu->actions().back()->setText(QString("%1").arg(idM));
        menu->actions().front()->triggered();
    }
    else if(ui->W->isVisible()&&ui->W->geometry().contains(event->pos())){
        qDebug()<<"W D";
        menu->actions().back()->setText(QString("%1").arg(idW));
        menu->actions().front()->triggered();
    }
}

void TreeInfoItem::mousePressEvent(QMouseEvent *event){
    if(event->button()==Qt::RightButton){
        if(ui->M->isVisible()&&ui->M->geometry().contains(event->pos())){
            //判断是否有伴侣，若无打开添加伴侣选项和关闭添加子女选项
            if(idW < 0){
                menu->actions()[2]->setDisabled(true);
                menu->actions()[3]->setEnabled(true);
            }
            else{
                menu->actions()[2]->setEnabled(true);
                menu->actions()[3]->setDisabled(true);
            }
            menu->actions().back()->setText(QString("%1").arg(idM));
            menu->exec(mapToGlobal(event->pos()));
        }
        else if(ui->W->isVisible()&&ui->W->geometry().contains(event->pos())){
            menu->actions()[2]->setEnabled(false);
            menu->actions()[3]->setEnabled(false);
            menu->actions().back()->setText(QString("%1").arg(idW));
            menu->exec(mapToGlobal(event->pos()));
        }
    }
}
